#include "home.h"
#include "ui_home.h"
#include "overview.h"
#include "activityeditor.h"
#include "querypage.h"

#include <QFile>
#include <QJsonDocument>
#include <QJsonParseError>

Home::Home(MODE mode,QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Home)
{
    ui->setupUi(this);
    int numButtons=0;
    if(mode&ResultOverview) {
        ui->overviewBtn->show();
        numButtons++;
    }
    else ui->overviewBtn->hide();
    if(mode&EditActivity) {
        ui->editBtn->show();
        numButtons++;
    }
    else ui->editBtn->hide();
    if(mode&QueryResult) {
        ui->queryBtn->show();
        numButtons++;
    }
    else ui->queryBtn->hide();
    this->setMaximumHeight(350+30*numButtons);
    this->setMinimumHeight(350+30*numButtons);
}

Home::~Home()
{
    delete ui;
}

void Home::on_overviewBtn_clicked()
{
    Overview *overview = new Overview;
    overview->home = this;

    QFile localData("data.enc.txcs");
    if(localData.exists() && localData.open(QFile::ReadOnly)) {
        QByteArray byteArray=decrypt(localData.readAll());
        localData.close();

        QJsonParseError parseError;
        QJsonDocument doc = QJsonDocument::fromJson(byteArray,&parseError);
        if (!doc.isEmpty() && parseError.error == QJsonParseError::NoError) {
            QJsonObject object = doc.object();
            if(object["students"].isArray()) {
                QList<Student> students = Student::getList(object["students"].toArray());
                foreach(Student student,students) {
                    overview->students[student.id]=student;
                }
            }
            if(object["activities"].isArray()) {
                QList<Activity> activities = Activity::getList(object["activities"].toArray());
                foreach(Activity activity,activities) {
                    overview->activities[activity.name]=activity;
                }
            }
        }
    }

    overview->refreshStu();
    overview->refreshAct();
    overview->refreshDat();
    overview->refreshOview(10);
    overview->show();
    this->hide();
}


void Home::on_editBtn_clicked()
{
    ActivityEditor *editor = new ActivityEditor;
    editor->home=this;

    QFile localData("stulist.enc.txcs");
    if(localData.exists() && localData.open(QFile::ReadOnly)) {
        QByteArray byteArray=decrypt(localData.readAll());
        localData.close();

        QJsonParseError parseError;
        QJsonDocument doc = QJsonDocument::fromJson(byteArray,&parseError);
        if (!doc.isEmpty() && parseError.error == QJsonParseError::NoError) {
            QJsonObject object = doc.object();
            if(object["students"].isArray()) {
                QList<Student> students = Student::getList(object["students"].toArray());
                foreach(Student student,students) {
                    editor->students[student.id]=student;
                }
            }
        }
    }
    editor->refreshStu();
    editor->refreshAct();
    editor->show();
    this->hide();
}


void Home::on_queryBtn_clicked()
{
    QueryPage *page = new QueryPage;
    page->home=this;

    QFile stuData("data.encstu.txcs");

    if(stuData.exists() && stuData.open(QFile::ReadOnly)) {
        QByteArray byteArray=decrypt(stuData.readAll(),true);
        stuData.close();

        QJsonParseError parseError;
        QJsonDocument doc = QJsonDocument::fromJson(byteArray,&parseError);
        if (!doc.isEmpty() && parseError.error == QJsonParseError::NoError) {
            QJsonObject object = doc.object();
            if(object["students"].isArray()) {
                QList<Student> students = Student::getList(object["students"].toArray());
                foreach(Student student,students) {
                    page->students[student.id]=student;
                }
            }
            if(object["activities"].isArray()) {
                QList<Activity> activities = Activity::getList(object["activities"].toArray());
                foreach(Activity activity,activities) {
                    page->activities[activity.name]=activity;
                }
            }
        }
    }

    page->show();
    this->hide();
}

